package com.lmuse.xmr.foodspotting.ui.userpage;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;

import com.dexafree.materialList.card.Card;
import com.dexafree.materialList.card.CardProvider;
import com.dexafree.materialList.listeners.OnDismissCallback;
import com.dexafree.materialList.listeners.RecyclerItemClickListener;
import com.dexafree.materialList.view.MaterialListView;
import com.lmuse.xmr.foodspotting.R;
import com.lmuse.xmr.foodspotting.db.Contract;
import com.lmuse.xmr.foodspotting.db.RecipePresenterImpl;
import com.lmuse.xmr.foodspotting.db.UserPresenterImpl;
import com.lmuse.xmr.foodspotting.model.Recipe;
import com.lmuse.xmr.foodspotting.model.User;
import com.lmuse.xmr.foodspotting.ui.recipe.RecipeActivity;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;

public class MyUploadActivity extends AppCompatActivity implements Contract.RecipeView, Contract.UserView{

    private MaterialListView cardList;

    private Contract.UserPresenter userPresenter;
    private Contract.RecipePresenter recipePresenter;

    private List<Recipe> recipeList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_upload);
        recipePresenter = new RecipePresenterImpl(this);
        userPresenter = new UserPresenterImpl(this);
        initView();
        recipeList = new ArrayList<>();
        userPresenter.getUserInfoCache();
    }

    void initView() {
        cardList = (MaterialListView) findViewById(R.id.cardlist_upload);
        cardList.addOnItemTouchListener(new CardClickListener());
        cardList.setOnDismissCallback(new CardDismissCallBack());
    }

    class CardClickListener implements RecyclerItemClickListener.OnItemClickListener {

        @Override
        public void onItemClick(@NonNull Card card, int position) {
            Intent startIntent = new Intent(MyUploadActivity.this,
                    RecipeActivity.class);
            startIntent.putExtra(com.lmuse.xmr.foodspotting.constants.Recipe.ID,
                    recipeList.get(position).getObjectId());
            startActivity(startIntent);
        }

        @Override
        public void onItemLongClick(@NonNull Card card, int position) {

        }
    }

    class CardDismissCallBack implements OnDismissCallback {
        @Override
        public void onDismiss(@NonNull Card card, int position) {
            /*int count = (page + 1) * limit - 2;
            if (position == count) {
                Toast.makeText(getActivity(), "当前第" + count + "个", Toast.LENGTH_SHORT).show();
                page++;
                recipePresenter.queryRecipe("label", labelTitle.getText().toString(), limit, page);
            }*/
        }
    }

    @Override
    public void onSignUpSuccess(User user) {

    }

    @Override
    public void onSignUpFailed(String msg) {

    }

    @Override
    public void onSignInSuccess(User user) {

    }

    @Override
    public void onSignInFailed(String msg) {

    }

    @Override
    public void onUserInfoCache(User user) {
        recipePresenter.queryRecipe("author", user.getObjectId(), 500);
    }

    @Override
    public void onUpdateUserSuccess() {

    }

    @Override
    public void onUpdateUserFailed(String msg) {

    }

    @Override
    public void onRequestEmailVerifyResult(BmobException e) {

    }

    @Override
    public void onResetPasswordByEmailResult(BmobException e) {

    }

    @Override
    public void onQueryUserByIdSuccess(User user) {

    }

    @Override
    public void onQueryUserByIdFailed(String msg) {

    }

    @Override
    public void onQueryUserAmountSuccess(int size) {

    }

    @Override
    public void onQueryUserAmountFailed(String msg) {

    }

    @Override
    public void onUploadingImg(Integer value) {

    }

    @Override
    public void onUploadingImg(int curIndex, int curPercent, int total, int totalPercent) {

    }

    @Override
    public void onUploadImgSuccess(BmobFile cover) {

    }

    @Override
    public void onUploadImgSuccess(List<String> urls) {

    }

    @Override
    public void onUploadImgFailed(String msg) {

    }

    @Override
    public void onAddRecipeSuccess() {

    }

    @Override
    public void onAddRecipeFailed(String msg) {

    }

    @Override
    public void onQueryRecipeSuccess(Recipe recipe) {

    }

    @Override
    public void onQueryRecipeSuccess(List<Recipe> recipeList) {
        this.recipeList = recipeList;

        for (Recipe recipe : recipeList) {
            Card card = new Card.Builder(this)
                    .setTag("BIG_IMAGE_CARD")
                    .withProvider(new CardProvider())
                    .setLayout(R.layout.material_big_image_card_layout)
                    .setTitle(recipe.getTitle())
                    .setTitleColor(getResources().getColor(R.color.colorWhite))
                    .setDrawable(recipe.getCoverUrl())
                    .endConfig()
                    .build();
            cardList.getAdapter().add(card);
        }
        cardList.smoothScrollToPosition(0);
    }

    @Override
    public void onQueryRecipeFailed(String msg) {

    }

    @Override
    public void onQueryKeySuccess(List<Recipe> recipeKeyList) {

    }

    @Override
    public void onQueryKeyFailed(String msg) {

    }

    @Override
    public void onDeleteRecipeSuccess() {

    }

    @Override
    public void onDeleteRecipeFailed(String msg) {

    }

    @Override
    public void onQueryByKeySuccess(List<User> userList) {

    }

    @Override
    public void onQueryByKeyFailed(String msg) {

    }


}
